<template>
  <div>
    <el-form :inline="true">
      <el-form-item>
        <el-date-picker
            v-model="searchForm.departDate"
            type="date"
            placeholder="揽件日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-select v-model="searchForm.status" class="filter-item" placeholder="Please select status">
          <el-option v-for="item in statusOption" :key="item.key" :label="item.value" :value="item.key" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="getLogisticsList">搜索</el-button>
      </el-form-item>
    </el-form>

    <el-divider><h1>包裹物流记录</h1></el-divider>
    <br>
    <div v-if="this.tableData.length===0">
      <el-empty description="没有更多物流信息咯"></el-empty>
    </div>
    <div v-else>
      <el-table
          :data="tableData"
          tooltip-effect="dark"
          style="width: 100%"
          border
          stripe>

        <el-table-column
            prop="logisticsId"
            label="物流编号"
            width="180">
        </el-table-column>
        <el-table-column
            prop="thisSite"
            label="揽件站点"
            width="220">
        </el-table-column>
        <el-table-column
            prop="targetSite"
            label="目标站点"
            width="220">
        </el-table-column>
        <el-table-column
            prop="departDate"
            label="揽件日期"
            width="200">
          <template slot-scope="scope">
            <el-text>{{scope.row.departDate | dateFilter}}</el-text>
          </template>
        </el-table-column>
        <el-table-column
            prop="arriveDate"
            label="送达日期"
            width="200">
          <template slot-scope="scope">
            <el-text>{{scope.row.arriveDate | dateFilter}}</el-text>
          </template>
        </el-table-column>

        <el-table-column
            prop="defaults"
            label="当前状态"
            width="80"
        >
          <template slot-scope="scope">
            <el-tag size="small" v-if="scope.row.status===0" type="warning">派送中</el-tag>
            <el-tag size="small" v-if="scope.row.status===1" type="success">已送达</el-tag>
          </template>
        </el-table-column>
        <el-table-column
            prop="icon"
            label="操作">

          <template slot-scope="scope" v-if="scope.row.status===0">
            <el-button type="text" @click="editHandle(scope.row.logisticsId)">修改目的地</el-button>
            <el-divider direction="vertical"></el-divider>
            <el-button type="text" @click="serviced(scope.row.logisticsId)">送达入库</el-button>
          </template>
        </el-table-column>

      </el-table>

      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          layout="total, sizes, prev, pager, next, jumper"
          :page-sizes="[10, 20, 50, 100]"
          :current-page="current"
          :page-size="size"
          :total="total">
      </el-pagination>
    </div>

    <!--新增对话框-->
    <el-dialog
        title="提示"
        :visible.sync="dialogVisible"
        width="600px"
        :before-close="handleClose">

      <el-form :model="editForm" :rules="editFormRules" ref="editForm">
        <el-form-item label="揽件站点" prop="thisSite" label-width="140px">
          <el-input v-model="editForm.thisSite" autocomplete="off" disabled></el-input>
        </el-form-item>
        <el-form-item label="目标站点" prop="targetSite" label-width="140px">
          <el-select v-model="editForm.targetSite" class="filter-item" placeholder="Please select station" filterable>
            <el-option v-for="item in stationOption" :key="item.key" :label="item.value" :value="item.key" />
          </el-select>
        </el-form-item>
        <el-form-item label="最终地点" prop="destination" label-width="140px">
          <el-switch v-model="editForm.destination" :active-value="true" :inactive-value="false" />
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="resetForm('editForm')">取 消</el-button>
        <el-button type="primary" @click="submitForm('editForm')">确 定</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import {getSendingList, servicedExpress, getLogisticsInfo, updateLogistics, getStationList} from "@/api/dm";

export default {
  name: "Sending",
  data() {
    return {
      tableData: [],
      searchForm: {},
      editForm: {},
      editFormRules: {
        targetSite: [
          {required: true, message: '请选择站点信息', trigger: 'blur'}
        ]
      },
      dialogVisible: false,
      total: 0,
      size: 10,
      current: 1,
      stationOption: [
      ],
      statusOption: [
        { key: 0, value: '派送中'},
        { key: 1, value: '已送达'}
      ]
    }
  },
  created() {
    this.getLogisticsList()
    this.getStationList()
  },
  methods: {
    getLogisticsList(){
      getSendingList(this.searchForm.departDate, this.searchForm.status).then(res =>{
        this.tableData = res.data.data.records
        this.size = res.data.data.size
        this.current = res.data.data.current
        this.total = res.data.data.total
      })
    },
    getStationList(){
      getStationList().then(res =>{
        res.data.data.forEach(stationItem =>{
          this.stationOption.push({ key: stationItem.address, value: stationItem.address})
        })
      })
    },
    handleSizeChange(val) {
      this.size = val
      this.getLogisticsList()
    },
    handleCurrentChange(val) {
      this.current = val
      this.getLogisticsList()
    },
    serviced(id){
      console.log(id)
      servicedExpress(id).then(res =>{
        this.$message({
          showClose: true,
          message: '恭喜你，操作成功',
          type: 'success',
          onClose:() => {
            this.getLogisticsList()
          }
        });
      })
    },
    editHandle(id){
      getLogisticsInfo(id).then(res =>{
        this.editForm = res.data.data
        this.dialogVisible = true
      })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
      this.dialogVisible = false
      this.editForm = {}
    },
    handleClose() {
      this.resetForm('editForm')
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          updateLogistics(this.editForm).then(res =>{
            this.$message({
              showClose: true,
              message: '恭喜你，操作成功',
              type: 'success',
              onClose:() => {
                this.getLogisticsList()
              }
            });
            this.dialogVisible = false
          })
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    }
  }
}
</script>

<style scoped>
.el-pagination {
  float: right;
  margin-top: 22px;
}
</style>